#!/usr/bin/python2.4
#
# Copyright 2011 Google Inc. All Rights Reserved.

from google.appengine.dist import use_library
use_library('django', '1.2')
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from gaesessions import get_current_session
from db import user
from libs import login_util

import os
import urllib

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

class MainPage(webapp.RequestHandler):
    def post(self):
        email = self.request.get('email')
        password = self.request.get('password')
        
        user_profile = user.VerifyUser(email, password)
        if user_profile:
            session = get_current_session()
            if session.is_active():
                session.terminate()
            login_util.LoginUtil.SetUserLogin(user_profile.email)
            session.regenerate_id()
            self.redirect('/')
        else:
            self.redirect('/staticpages/error.html?msg=' + urllib.quote('Your email/password is not correct!'))


application = webapp.WSGIApplication([('/signin.do', MainPage)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
